package com.shbaiche.bifenba.ui.match;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.data.DataIntegralListAdapter;
import com.shbaiche.bifenba.adapter.match.MatchListAdapter;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.model.data.StandingMessage;
import com.shbaiche.bifenba.model.data.StandingVo;
import com.shbaiche.bifenba.model.match.MatchesListMessage;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.base.XLazyFragment;
import cn.droidlover.xdroidbase.kit.Kits;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * 积分
 */

@SuppressLint("ValidFragment")
public class MatchListFragment extends XLazyFragment {

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    MatchListAdapter adapter;

    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 10;
    CompeteVo mCompeteVo;

    public MatchListFragment(){

    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("data")){
            mCompeteVo = (CompeteVo)savedInstanceState.getSerializable("data");
        }else{
            mCompeteVo = (CompeteVo)getArguments().getSerializable("data");
        }
        initAdapter();
        loadData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event.LoginEvent event) {
        if (event != null) {
            loadData();
        }
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData();
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData();
                    }
                });

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();

        contentLayout.getEmptyView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });

    }

    public void loadData() {
        contentLayout.showLoading();
        if(mCompeteVo!=null && mCompeteVo.getId().equals("-5")){
            if(App.getPassport()==null){
                contentLayout.showEmpty();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                View view = contentLayout.getEmptyView();
                if(view!=null){
                    TextView tv = (TextView) view.findViewById(R.id.tv_empty);
                    tv.setText("您还未登录，请先登录。");
                    getAdapter().setData(new ArrayList());
                }
                return;
            }
        }
        MatchesListMessage message = new MatchesListMessage();
        message.setPassport(App.getPassport());
        message.setCompeteId(mCompeteVo.getId());
        message.setGameType(mCompeteVo.getGameType());

        NetApi.matchesList(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                MatchesListMessage result =new Gson().fromJson(response.getMessage(),MatchesListMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null && result.getVoMap()!=null){
                        Log.e("t","onResponse");
                        ArrayList<MatchesVo> list = new ArrayList<MatchesVo>();
                        MatchesVo vo;
                        for (String key : result.getVoMap().keySet()){
                            //list.addAll(result.getVoMap().get(key));
                            for (int i=0;i<result.getVoMap().get(key).size();i++){
                                vo = result.getVoMap().get(key).get(i);
                                vo.dateShow = key;
                                list.add(vo);
                            }
                        }
                        getAdapter().setData(list);

                        if (getAdapter().getItemCount() < 1) {
                            contentLayout.showEmpty();
                            return;
                        }
                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                    ToastUtil.show(result.getMessage());
                }
            }
        });





    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new MatchListAdapter(getActivity());
            adapter.setRecItemClick(new RecyclerItemCallback<MatchesVo, MatchListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, MatchesVo model, int tag, MatchListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_match_list;
    }

}
